package com.linewell.system.controller;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import com.linewell.commom.controller.BackendBaseController;
import com.linewell.system.model.SysCustomer;
import com.linewell.system.model.SysDepartment;
import com.linewell.system.model.SysRoles;
import com.linewell.system.pagemodel.DataGrid;
import com.linewell.system.pagemodel.PageHelper;
import com.linewell.system.pagemodel.ReturnInfo;
import com.linewell.system.pagemodel.SimpleTreeNode;
import com.linewell.system.service.ISysCustomerService;
import com.linewell.system.utils.SimpleTreeUtil;

/**
 * @系统用户控制器 
 */
@Controller
@EnableWebMvc
@RequestMapping("/manage/sysCustomer")
public class SysCustomerController extends BackendBaseController {

	private static final Log log = LogFactory.getLog(SysCustomerController.class);

	@Autowired
	private ISysCustomerService sysCustomerService;

	/**
	 * 跳转至 syscustomer页
	 */
	@RequestMapping(value = "/index")
	public String index(HttpServletRequest request) {
		return super.index("/WEB-INF/pages/system/sys_customer/sys_customer_list.jsp", request);
	}

	/**
	 * 查询客户信息
	 */
	@ResponseBody
	@RequestMapping(value = "/dataGrid")
	public DataGrid dataGrid(@ModelAttribute SysCustomer c,PageHelper ph,HttpServletRequest request) {
		ph.setSort("custId");
		ph.setOrder("desc");
		DataGrid dg = sysCustomerService.datagrid(ph,c,getUserDepId(request));
		return dg;
	}

	/**
	 * 跳转至 syscustomer_au页(添加和修改共用一个编辑页)
	 */
	@RequestMapping(value = "/add")
	public String add(Map<String, Object> map) {
		map.put("saveUrl", "/manage/sysCustomer/addSave");
		return "/WEB-INF/pages/system/sys_customer/sys_customer_au.jsp";
	}

	/**
	 * 保存添加数据
	 */
	@ResponseBody
	@RequestMapping(value = "/addSave")
	public ReturnInfo addSave(@Valid SysCustomer sysCustomer, BindingResult b, ReturnInfo result) {
		if (b.getErrorCount() > 0) { // 数据校验
			for (FieldError error : b.getFieldErrors()) {
				log.info(error.getField() + ":" + error.getDefaultMessage());
				result.setMsg(error.getDefaultMessage());
			}
			result.setSuccess(1);
		} else {
			if (sysCustomer != null) {
				//log.info(sysCustomer.getEmail());
				log.info(sysCustomer.getSysDepartment().getDepId());
				try {
					sysCustomerService.save(sysCustomer);
				} catch (Exception e) {
					log.info("保存失败" + e.getMessage());
					result.setMsg("保存失败！");
					if(sysCustomer.getSysDepartment().getDepId() == 0){
						result.setMsg("请选择部门！");
					}
					result.setSuccess(1);
				}
			}
		}

		return result;
	}

	/**
	 * 跳转至 syscustomer_au页
	 */
	@RequestMapping(value = "/edit")
	public String edit(String id, Map<String, Object> map) {
		log.info(id);
		map.put("saveUrl", "/manage/sysCustomer/editSave");
		if (id != null) {
			List<SysCustomer> model = sysCustomerService.getUser(id);
			map.put("model", model.get(0));
		}
		return "/WEB-INF/pages/system/sys_customer/sys_customer_au.jsp";
	}	
	/**
	 * 跳转至 syscustomer页
	 */
	@ResponseBody
	@RequestMapping(value = "/editSave")
	public ReturnInfo editSave(@Valid SysCustomer sysCustomer, BindingResult b, ReturnInfo result) {
		if (b.getErrorCount() > 0) { // 数据校验
			for (FieldError error : b.getFieldErrors()) {
				log.info(error.getField() + ":" + error.getDefaultMessage());
				result.setMsg(error.getDefaultMessage());
			}
			result.setSuccess(1);
		} else {
			if (sysCustomer != null) {
				try {
					log.info("部门"+sysCustomer.getSysDepartment().getDepId());
					sysCustomerService.update(sysCustomer);
					//sysCustomerService.saveOrUpdate(sysCustomer);
				} catch (Exception e) {
					log.info("修改异常" + e.getMessage());
					result.setMsg("修改失败");
					if(sysCustomer.getSysDepartment().getDepId() == 0){
						result.setMsg("请选择部门！");
					}
					result.setSuccess(1);
				}

			}
		}

		return result;
	}

	/**
	 * 物理删除,从数据库中删除
	 */
	@ResponseBody
	@RequestMapping(value = "/delete")
	public ReturnInfo delete(String ids, ReturnInfo result) {
		if (ids != null) {
			try {
				sysCustomerService.deleteAll(ids);
			} catch (Exception e) {
				log.info("删除异常" + e.getMessage());
				result.setMsg("删除失败！");
				result.setSuccess(1);
			}
		}

		return result;
	}

	/**
	 * 移除,逻辑删除，将状态修改为0
	 */
	@ResponseBody
	@RequestMapping(value = "/remove")
	public ReturnInfo remove(String ids, ReturnInfo result) {
		log.info(ids);
		if (ids != null) {
			try {
				sysCustomerService.removeAll(ids);
			} catch (Exception e) {
				log.info("移除异常" + e.getMessage());
				result.setMsg("移除失败！");
				result.setSuccess(1);
			}
		}

		return result;
	}	
	/**
	 * 获得客户详细资料
	 */
	@RequestMapping(value = "/queryCustDetail")
	public String queryCustDetail(String id, Map<String, Object> map) {
		log.info(id);
		if (id != null) {
			List<SysCustomer> model = sysCustomerService.getUser(id);
			map.put("model", model.get(0));
		}
		return "/WEB-INF/pages/system/sys_customer/sys_customer_detail.jsp";
	}
	

}
